/**
 * 配置文件
 * 包含系统配置参数和PLC设备配置
 */
require('dotenv').config();

const config = {
  // 系统配置
  system: {
    logLevel: process.env.LOG_LEVEL || 'info',
    logFile: process.env.LOG_FILE || 'logs/plc-communication.log',
    dataStoragePath: process.env.DATA_STORAGE_PATH || 'data/',
    reconnectInterval: parseInt(process.env.RECONNECT_INTERVAL || '5000', 10),
    maxReconnectAttempts: parseInt(process.env.MAX_RECONNECT_ATTEMPTS || '10', 10),
    heartbeatInterval: parseInt(process.env.HEARTBEAT_INTERVAL || '30000', 10),
  },
  
  // Modbus TCP配置
  modbusTCP: {
    defaultPort: parseInt(process.env.MODBUS_TCP_PORT || '502', 10),
    timeout: parseInt(process.env.MODBUS_TCP_TIMEOUT || '5000', 10),
    autoReconnect: process.env.MODBUS_TCP_AUTO_RECONNECT !== 'false',
  },
  
  // Modbus RTU配置
  modbusRTU: {
    defaultBaudRate: parseInt(process.env.MODBUS_RTU_BAUDRATE || '9600', 10),
    defaultParity: process.env.MODBUS_RTU_PARITY || 'none',
    defaultStopBits: parseInt(process.env.MODBUS_RTU_STOPBITS || '1', 10),
    defaultDataBits: parseInt(process.env.MODBUS_RTU_DATABITS || '8', 10),
    timeout: parseInt(process.env.MODBUS_RTU_TIMEOUT || '5000', 10),
  },
  
  // PLC设备配置
  devices: [
    // 西门子S7-1200示例配置
    {
      id: 'siemens-s7-1200-1',
      name: 'S7-1200 PLC 1',
      brand: 'Siemens',
      model: 'S7-1200',
      protocol: 'ModbusTCP',
      ip: process.env.SIEMENS_S7_1200_1_IP || '192.168.0.2',
      port: parseInt(process.env.SIEMENS_S7_1200_1_PORT || '2000', 10),
      unitId: parseInt(process.env.SIEMENS_S7_1200_1_UNIT_ID || '1', 10),
      pollInterval: parseInt(process.env.SIEMENS_S7_1200_1_POLL_INTERVAL || '1000', 10),
      tags: [
        { name: 'Temperature', address: '40001', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.1 },
        { name: 'Pressure', address: '40003', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.01 },
        { name: 'Valve1Status', address: '00001', type: 'coil', dataType: 'boolean' },
        { name: 'Valve2Status', address: '00002', type: 'coil', dataType: 'boolean' },
      ],
    },
    
    // // 西门子S7-1500示例配置
    // {
    //   id: 'siemens-s7-1500-1',
    //   name: 'S7-1500 PLC 1',
    //   brand: 'Siemens',
    //   model: 'S7-1500',
    //   protocol: 'ModbusTCP',
    //   ip: process.env.SIEMENS_S7_1500_1_IP || '192.168.1.11',
    //   port: parseInt(process.env.SIEMENS_S7_1500_1_PORT || '502', 10),
    //   unitId: parseInt(process.env.SIEMENS_S7_1500_1_UNIT_ID || '1', 10),
    //   pollInterval: parseInt(process.env.SIEMENS_S7_1500_1_POLL_INTERVAL || '1000', 10),
    //   tags: [
    //     { name: 'ProductionCount', address: '40010', type: 'holdingRegister', dataType: 'int32' },
    //     { name: 'MotorSpeed', address: '40012', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.1 },
    //     { name: 'EmergencyStop', address: '00010', type: 'coil', dataType: 'boolean' },
    //     { name: 'SystemStatus', address: '10001', type: 'discreteInput', dataType: 'boolean' },
    //   ],
    // },
    
    // // 罗克韦尔1769示例配置
    // {
    //   id: 'rockwell-1769-1',
    //   name: 'Rockwell 1769 PLC 1',
    //   brand: 'Rockwell',
    //   model: '1769',
    //   protocol: 'ModbusTCP',
    //   ip: process.env.ROCKWELL_1769_1_IP || '192.168.1.20',
    //   port: parseInt(process.env.ROCKWELL_1769_1_PORT || '502', 10),
    //   unitId: parseInt(process.env.ROCKWELL_1769_1_UNIT_ID || '1', 10),
    //   pollInterval: parseInt(process.env.ROCKWELL_1769_1_POLL_INTERVAL || '1000', 10),
    //   tags: [
    //     { name: 'TankLevel', address: '40020', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.1 },
    //     { name: 'FlowRate', address: '40022', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.01 },
    //     { name: 'PumpStatus', address: '00020', type: 'coil', dataType: 'boolean' },
    //     { name: 'AlarmStatus', address: '10010', type: 'discreteInput', dataType: 'boolean' },
    //   ],
    // },
    
    // // 罗克韦尔1756示例配置
    // {
    //   id: 'rockwell-1756-1',
    //   name: 'Rockwell 1756 PLC 1',
    //   brand: 'Rockwell',
    //   model: '1756',
    //   protocol: 'ModbusRTU',
    //   port: process.env.ROCKWELL_1756_1_PORT || '/dev/ttyUSB0',
    //   baudRate: parseInt(process.env.ROCKWELL_1756_1_BAUDRATE || '9600', 10),
    //   parity: process.env.ROCKWELL_1756_1_PARITY || 'none',
    //   stopBits: parseInt(process.env.ROCKWELL_1756_1_STOPBITS || '1', 10),
    //   dataBits: parseInt(process.env.ROCKWELL_1756_1_DATABITS || '8', 10),
    //   unitId: parseInt(process.env.ROCKWELL_1756_1_UNIT_ID || '1', 10),
    //   pollInterval: parseInt(process.env.ROCKWELL_1756_1_POLL_INTERVAL || '1000', 10),
    //   tags: [
    //     { name: 'Temperature', address: '40030', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.1 },
    //     { name: 'Pressure', address: '40032', type: 'holdingRegister', dataType: 'float', scaleFactor: 0.01 },
    //     { name: 'ValveStatus', address: '00030', type: 'coil', dataType: 'boolean' },
    //     { name: 'MotorStatus', address: '10020', type: 'discreteInput', dataType: 'boolean' },
    //   ],
    // },
  ],
};

module.exports = config;
